package com.itsq.service.resources.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsq.common.bean.ErrorEnum;
import com.itsq.common.constant.APIException;
import com.itsq.pojo.dto.PageCompanyDto;
import com.itsq.pojo.entity.Change;
import com.itsq.pojo.entity.Company;
import com.itsq.mapper.CompanyMapper;
import com.itsq.service.resources.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-01-29
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired(required = false)
    private CompanyMapper companyMapper;

    @Override
    public Page<Company> selCompanyPageFront(PageCompanyDto pageCompanyDto) {
        Page<Company> page=new Page<>(pageCompanyDto.getPageIndex(),pageCompanyDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageCompanyDto.getOrderByField()==null){
            queryWrapper.orderByDesc("cre_date");
        }else{
            queryWrapper.orderByDesc(pageCompanyDto.getOrderByField());
        }
        return companyMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<Company> selCompanyPageAfter(PageCompanyDto pageCompanyDto) {
        Page<Company> page=new Page<>(pageCompanyDto.getPageIndex(),pageCompanyDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageCompanyDto.getOrderByField()==null&&pageCompanyDto.getName()==null){
            queryWrapper.orderByDesc("cre_date");
        }else if(pageCompanyDto.getName()!=null){
            queryWrapper.orderByDesc("cre_date");
            queryWrapper.like("name",pageCompanyDto.getName());
        }else{
            queryWrapper.orderByDesc(pageCompanyDto.getOrderByField());
        }
        return companyMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<Company> selCompanyList() {
        QueryWrapper queryWrapper=new QueryWrapper();
        return companyMapper.selectList(queryWrapper);
    }

    @Override
    public Company selCompanyId(Integer id) {
        return companyMapper.selectById(id);
    }

    @Override
    public Integer insert(Company company) {
        int i = companyMapper.insert(company);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }
        return i;
    }

    @Override
    public Integer update(Company company) {
        int i = companyMapper.updateById(company);
        if(i<1){
            throw new APIException(ErrorEnum.is_UPDATE);
        }
        return i;
    }

    @Override
    public Integer delete(Integer id) {
        return this.baseMapper.deleteById(id);
    }
}
